package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"product/model/mysql"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *productrpc.UpdateRequest) (*productrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var findpro mysql.Product
	mysql.DB.Where("id=?", in.Id).Find(&findpro)
	if findpro.ID == 0 {
		return nil, fmt.Errorf("该账号还没注册，请先去注册")
	}

	var pro = mysql.Product{
		Model:    gorm.Model{ID: uint(in.Id)},
		Username: in.Username,
		Password: in.Password,
		Name:     in.Name,
		Price:    in.Img,
	}
	mysql.DB.Updates(&pro)

	return &productrpc.UpdateResponse{Id: int64(pro.ID)}, nil
}
